﻿using FANUC.DTO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class RobotStateChangeHistoryInquireController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        /// <summary>
        /// GET: api/ApiRobotStateChangeHistory/Robot_YH_No
        /// </summary>
        /// <param name="id">Robot_YH_No</param>
        /// <returns>PartUnitHistoryDtoOutput</returns>
        [ResponseType(typeof(PartUnitHistoryDtoOutput))]
        public IHttpActionResult Get(string id)
        {
            var robotStateChanges = db.Robot_State_Change_History.Where(a => a.Robot.YH_No == id).ToList();
            if (robotStateChanges.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Robot_State_Change_History, RobotStateChangeRecordDto>().ForMember(dest => dest.Original_State_Type, src => src.MapFrom(s => s.Robot_State_Type.Type)).ForMember(dest => dest.Changed_State_Type, src => src.MapFrom(s => s.Robot_State_Type.Type)));
            List<RobotStateChangeRecordDto> recordsDto = AutoMapper.Mapper.Map<List<RobotStateChangeRecordDto>>(robotStateChanges);
            RobotStateChangeHistoryDtoOutput data = new RobotStateChangeHistoryDtoOutput();
            data.Robot_YH_NO = id;
            data.RobotStateChangeRecords = recordsDto;

            return Ok(data);
        }

        /// <summary>
        /// GET: api/ApiRobotStateChangeHistory/?id=Robot_YH_No&pageNumber=1&pageSize=10
        /// </summary>
        /// <param name="id">Robot_YH_No</param>
        /// <param name="pageNumber">Page Number</param>
        /// <param name="pageSize">Page Size</param>
        /// <returns></returns>
        [ResponseType(typeof(PartUnitHistoryDtoOutput))]
        public IHttpActionResult Get(string id, int pageNumber, int pageSize)
        {
            var robotStateChanges = db.Robot_State_Change_History.Where(a => a.Robot.YH_No == id).OrderByDescending(a=>a.ChangeDateTime).Skip(pageNumber).Take(pageSize).ToList();
            if (robotStateChanges.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Robot_State_Change_History, RobotStateChangeRecordDto>().ForMember(dest=>dest.Original_State_Type,src=>src.MapFrom(s=>s.Robot_State_Type.Type)).ForMember(dest => dest.Changed_State_Type, src => src.MapFrom(s => s.Robot_State_Type.Type)));
            List<RobotStateChangeRecordDto> recordsDto = AutoMapper.Mapper.Map<List<RobotStateChangeRecordDto>>(robotStateChanges);
            RobotStateChangeHistoryDtoOutput data = new RobotStateChangeHistoryDtoOutput();
            data.Robot_YH_NO = id;
            data.RobotStateChangeRecords = recordsDto;

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        {
            return null;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
